home *** CD-ROM | disk | FTP | other *** search
/ NetNews Offline 2 / NetNews Offline Volume 2.iso / news / comp / lang / c-part1 / 5192 < prev    next >
Encoding:
Text File  |  1996-08-05  |  2.4 KB  |  93 lines

  1. Path: lrz-muenchen.de!news
  2. From: watzka@stat.uni-muenchen.de (Kurt Watzka)
  3. Newsgroups: comp.lang.c
  4. Subject: Re: typedefs for functions??
  5. Date: 8 Feb 1996 13:19:22 GMT
  6. Organization: Leibniz-Rechenzentrum, Muenchen (Germany)
  7. Distribution: world
  8. Message-ID: <4fct8q$2u1@sparcserver.lrz-muenchen.de>
  9. References: <4fa7u0$516@stork.runit.sintef.no>
  10. NNTP-Posting-Host: sun2.lrz-muenchen.de
  11.  
  12. phi@bh.marintek.sintef.no (Per Henning Isaksen) writes:
  13.  
  14.  
  15.  
  16. >The following code works as intended, but I would like to
  17. >change its 'looks'.
  18.  
  19. >     1
  20. >     2  typedef void (*ActionFunction)(char* g);
  21. >     3
  22. >     4  void a(char* b, ActionFunction g) {
  23. >     5       ActionFunction p=g;
  24. >     6       (*p)(b);
  25. >     7       return;
  26. >     8  }
  27. >     9  void aa(char* g) { /* an ActionFunction */
  28. >    10       printf("..%s..\n", g);
  29. >    11       return;
  30. >    12  }
  31. >    13
  32. >    14
  33. >    15  void main() {
  34. >    16       a("adf", aa);
  35. >    17  }
  36.  
  37. >Purpose: Inside a Motif GUI, I want to pass a function that
  38. >can perform some action, so I have the typdef in line 2 to
  39. >define ActionFunction. 
  40.  
  41. >I would like to change line 9 to:
  42. >   9  ActionFunction aa(char* g) {
  43. >But this causes a warning on line 16 (but it executes correctly)
  44. >   warning 604: Pointers are not assignment-compatible.
  45. >   warning 563: Argument #2 is not the correct type.
  46.  
  47. "aa" is a function taking one argument that is a pointer to char
  48. and returning a pointer to a function that takes one argument
  49. that is a pointer to char and returns nothing. This is obviously
  50. not the type for which "ActionFunction" is an alias. Hence the
  51. warnings.
  52.  
  53. >Of course, I could use a cast, but I wouldn't like it.
  54. >Anyone who can teach me the trick? 
  55.  
  56. The closest thing to what you want that comes to my mind is
  57. something like
  58.  
  59. --------------8<----------8<--------------8<-----------------
  60. #include <stdio.h>
  61.  
  62. typedef int f(int, int);
  63.  
  64. typedef int (* pf)(int, int);
  65.  
  66. f sum;   /* This is a declaration for sum. Most compilers are
  67.             able to warn you it the definition of a function
  68.             does not match it's declaration.                 */
  69.  
  70. int 
  71. sum(int a, int b)
  72. {
  73.    return a + b;
  74. }
  75.  
  76. int
  77. main()
  78. {
  79.    pf func;
  80.  
  81.    func = sum;
  82.    printf("%d\n", (* func)(2, 3));
  83.    return 0;
  84. }
  85. --------------8<----------8<--------------8<-----------------
  86.  
  87. Kurt
  88. --
  89. | Kurt Watzka                             Phone : +49-89-2180-6254
  90. | watzka@stat.uni-muenchen.de
  91. | ua302aa@sunmail.lrz-muenchen.de
  92.  
  93.